var express = require("express")
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.listen('8081', function() {
    console.log("http://localhost:8081");
})

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "content-type,authorization");
    //res.header("Access-Control-Allow-Headers", "authorization");
    next();
})
var mysql = require("mysql");
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const res = require("express/lib/response");

function query(sql, sqlParam, callback) {
    var connection = mysql.createConnection({
        url: 'localhost',
        port: 3306,
        user: 'root',
        password: '123456',
        database: 'vue_cart'
    });
    connection.connect()
    connection.query(sql, sqlParam, function(err, result) {
        if (err) {
            console.log('mysql err\n');
            console.log(err);
        }
        callback(err, result);
    });
    connection.end();
}
var resultData = {
        msg: {
            code: '',
            message: ''
        },
        data: {}
    }
    //获取商品数据
app.post('/goods/list', function(req, res) {
    var sql = "select * from goods";
    var sqlParam = [];
    query(sql, sqlParam, function(err, result) {
        if (err) {
            resultData.msg.code = '5000';
            resultData.msg.message = err;
            res.send(resultData);
        } else {
            resultData.msg.code = '2000';
            resultData.msg.message = '查询成功';
            resultData.data = result;
            res.send(resultData);
            // console.log(resultData);
        }
    })
})
app.post('/goods/cut', function(req, res) {
    var sql = "select * from goods where id = ?";
    var sqlParam = [req.body.id];
    query(sql, sqlParam, function(err, result) {
        if (err) {
            resultData.msg.code = '5000';
            resultData.msg.message = err;
            res.send(resultData);
        } else {
            var results = strJsonToObj(result);
            if (results.length == 0) {
                //做添加的操作
                resultData.msg.code = '2000';
                resultData.msg.message = "没有该商品，无法操作";
                res.send(resultData);
            } else {
                //修改的操作
                if (results[0].goods_count == 0 || results[0].goods_count == null || results[0].goods_count < 0) {
                    resultData.msg.code = '2000';
                    resultData.msg.message = "没有足够的数量去减了,请你删除";
                    res.send(resultData);
                } else {
                    var nowCount = results[0].goods_count - 1;
                    var sql = "update goods set goods_count=? where id=?";
                    var sqlParam = [nowCount, results[0].id];
                    query(sql, sqlParam, function(err, result) {
                        if (err) {
                            resultData.msg.code = '5000';
                            resultData.msg.message = err;
                            res.send(resultData);
                        } else {
                            resultData.msg.code = '2000';
                            resultData.msg.message = '';
                            resultData.data = result;
                            res.send(resultData);
                        }
                    })
                }

            }
        }
    })
})
app.post('/goods/add', function(req, res) {
    var sql = "select * from goods where id = ?";
    var sqlParam = [req.body.id];
    query(sql, sqlParam, function(err, result) {
        if (err) {
            resultData.msg.code = '5000';
            resultData.msg.message = err;
            res.send(resultData);
        } else {
            var results = strJsonToObj(result);
            if (results.length == 0) {
                //做添加的操作
            } else {
                //修改的操作
                var nowCount = results[0].goods_count + 1;
                var sql = "update goods set goods_count=? where id=?";
                var sqlParam = [nowCount, results[0].id];
                query(sql, sqlParam, function(err, result) {
                    if (err) {
                        resultData.msg.code = '5000';
                        resultData.msg.message = err;
                        res.send(resultData);
                    } else {
                        resultData.msg.code = '2000';
                        resultData.msg.message = '添加成功';
                        resultData.data = result;
                        res.send(resultData);
                    }
                })
            }
        }
    })
})

function strJsonToObj(str) {
    var resultStr = JSON.stringify(str);
    return JSON.parse(resultStr);
}

//更新商品表里面的数量和单个总金额
app.post('/updateGoods', function(req, res) {
    var sql = "update goods set goods_count=?,goods_sum=?,goods_state=? where id=?";
    var sqlParam = [req.body.goods_count, req.body.goods_sum, req.body.goods_state, req.body.id];
    query(sql, sqlParam, function(err, result) {
        if (err) {
            resultData.msg.code = '5000';
            resultData.msg.message = err;
            res.send(resultData);
        } else {
            resultData.msg.code = '2000';
            resultData.msg.message = '查询成功';
            resultData.data = result;
            res.send(resultData);
        }
    })
})

app.post('/checkAll', function(req, res) {
    var sql = "update goods set goods_state=?";
    var sqlParam = [req.body.goods_state];
    query(sql, sqlParam, function(err, result) {
        if (err) {
            resultData.msg.code = '5000';
            resultData.msg.message = err;
            res.send(resultData);
        } else {
            resultData.msg.code = '2000';
            resultData.msg.message = '查询成功';
            resultData.data = result;
            res.send(resultData);
        }
    })
})